package top.sdwu_top.kbase.common.handler;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import top.sdwu_top.kbase.common.model.Wrapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class AjaxAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        log.info("鉴权失败");
        Map map = httpServletRequest.getParameterMap();
        Wrapper result = Wrapper.wrap(400, "登陆失败");
        String resultString = JSON.toJSONString(result);
        httpServletResponse.getWriter().write(JSON.toJSONString(Wrapper.wrap(400, "登陆失败")));
    }
}